import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.text.html.*;
import java.io.*;
import java.util.Vector;

/**
 * Main class for creating a web browser
 * 
 * @author Chia-Chi (Teresa) Lee
 */ 
public class Browser extends JPanel {
  
  //For tracking browser history
  private Vector history;
  
  /**
   * Create initial interface for the Browser tab
   * 
   */ 
  public void init(){
    String initialURL = "http://www.gatech.edu/";
    history = new Vector();
    history.add(initialURL);
    final JTextField txtURL = new JTextField(initialURL);
    final JButton btnBack = new JButton("Back");
    
    //Create top control panel, which includes the browser text field and a back button
    JPanel topControl = new JPanel();
    topControl.setLayout(new BorderLayout(5,0));
    topControl.add(btnBack, BorderLayout.WEST);
    btnBack.setEnabled(false);
    topControl.add(txtURL, BorderLayout.CENTER);
    
    //Create JEditorPane and set it up to display the initial web page
    try {
            final JEditorPane ed = new JEditorPane(initialURL);
            ed.setEditable(false);
            ed.addHyperlinkListener(new HyperlinkListener(){
              public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                  JEditorPane pane = (JEditorPane) e.getSource();
                  if (e instanceof HTMLFrameHyperlinkEvent) {
                    HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
                    HTMLDocument doc = (HTMLDocument) pane.getDocument();
                    doc.processHTMLFrameHyperlinkEvent(evt);
                  } else {
                    try {
                      pane.setPage(e.getURL());
                      txtURL.setText(e.getURL().toString());
                      history.add(e.getURL().toString());
                      //Re-enable the back button if it is disabled
                      btnBack.setEnabled(true);
                    } catch (Throwable t) {
                      t.printStackTrace();
                    }
                  }
                }
              }
            });

     //Add listener to the URL textfield so when a user hits enter, the browser will load the url 
     txtURL.addKeyListener(
            new KeyListener() {
               public void keyPressed(KeyEvent e) {
                   if(e.getKeyChar() == e.VK_ENTER) { 
                      try {
                         String url = txtURL.getText().trim();
                         
                         //Checking whether the user has http or https typed. If not, add http.
                         if(!url.contains("http://")&&!url.contains("https://"))
                           url = "http://" + url;
                         
                         ed.setPage(url);
                         history.add(url);
                         //Re-enable the back button if it is disabled
                         btnBack.setEnabled(true);
                       } catch (IOException ex) {
                          ex.printStackTrace();
                       }
                    }
                }
                        
                //unused abstract methods
                public void keyTyped(KeyEvent e) {}
                public void keyReleased(KeyEvent e) {}
            });
     
     //Set up the back button
     btnBack.addActionListener(
       new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           try {
              String current = (String)history.lastElement();
              //Remove the last element because it is the current url
              history.removeElement(current);
              current = (String)history.lastElement();
                 ed.setPage(current.trim());
                 txtURL.setText(current.trim());
                 //disable the back button when there is no history
                 if(history.size()<=1)
                   btnBack.setEnabled(false);
                 } catch (IOException ex) {
                   ex.printStackTrace();
                 }
               }
       });
            
       JScrollPane sp = new JScrollPane(ed);
       setLayout(new BorderLayout());
       add(topControl, BorderLayout.NORTH);
       add(sp, BorderLayout.CENTER);

    } catch (IOException ex) {
        ex.printStackTrace();
    }
  }
}